package com.captjack.micro.dao.mongodb.configuration;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;

/**
 * Description description.....
 *
 * @author Jack Sparrow
 * @version 1.0.0
 * @date 2018/5/20 23:18
 * package com.example.provide.configuration.mongo
 */
@Configuration
public class MongoDataBaseConfiguration {

    @Bean("mongoTemplate")
    MongoTemplate customizeMongoTemplate(MongoDbFactory simpleMongoDbFactory, ApplicationContext applicationContext) {
        MongoMappingContext mongoMappingContext = new MongoMappingContext();
        mongoMappingContext.setApplicationContext(applicationContext);
        // 取消spring-mongo自带的"_class"字段
        MappingMongoConverter converter = new MappingMongoConverter(new DefaultDbRefResolver(simpleMongoDbFactory), mongoMappingContext);
        converter.setTypeMapper(new DefaultMongoTypeMapper(null));
        // 获取操作模版
        return new MongoTemplate(simpleMongoDbFactory, converter);
    }

}
